package com.sonotec.scap.ventas.model.dao.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import com.sonotec.scap.common.util.Utilitario;
import com.sonotec.scap.ventas.model.TCliente;
import com.sonotec.scap.ventas.model.dao.EntidadDAO;

public class JPATClienteDAO implements EntidadDAO<TCliente>
{
	private EntityManager em;
	private EntityTransaction tx;
	private EntidadDAO<TCliente> clienteDAO;
	public JPATClienteDAO()
	{
		em = Utilitario.getInstance().getEntityManager();	
		 tx = em.getTransaction();	
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TCliente> findAll() {		
		Query q = em.createQuery("select o from TCliente o");					
		return q.getResultList();
	}

	@Override
	public TCliente find(int dni) {		
		TCliente cliente = em.find(TCliente.class, dni);
		return cliente;
	}

	@Override
	public TCliente findString(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public TCliente insert(TCliente o)
	{		
		try
		{		
			if(!tx.isActive())
				tx.begin();
			
			em.persist(o);
			tx.commit();			
		}
		catch (Exception ex)
		{
			tx.rollback();			
			return null;			
		}	
		
		return o;
	}

	@Override
	public TCliente update(TCliente o) {
		em.merge(o);
		
		return o;
		
	}

	@Override
	public void delete(TCliente o) {
		// TODO Auto-generated method stub		
	}

	@Override
	public int findMaxCod() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	

}
